package com.zy.ivideo.service;

import com.zy.ivideo.entity.AccountEntity;
import com.zy.ivideo.entity.ResponseEntity;
import com.zy.ivideo.entity.UserEntity;
import com.zy.ivideo.repository.AccountRepository;
import com.zy.ivideo.repository.UserRepository;
import com.zy.ivideo.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

@Service
public class UserService {
    @Autowired
    UserRepository userRepository;

    @Autowired
    EntityManager entityManager;

    @Autowired
    AccountRepository accountRepository;

    @Autowired
    TokenService tokenService;

    /**
     * 获取所有用户方法
     * @return
     */
    public ResponseEntity<List<UserEntity>> getUsers(){
        List<UserEntity> result = userRepository.findAll();
        if (result==null||result.size()==0){
            return ResponseUtils.failed("没有获取到符合条件的数据记录");
        }
        return ResponseUtils.success(result);
    }

    /**
     * 用户登录方法
     * @param username
     * @param password
     * @return
     */
    public ResponseEntity<UserEntity> login(String username,String password){
        String sql="select * from s_user where username='"+username+"' and password='"+password+"'";
        Query query = entityManager.createNativeQuery(sql, UserEntity.class);
        List<UserEntity> result =  query.getResultList();

        if (result!=null&&result.size()>0){
            UserEntity userEntity=result.get(0);
            String userToken = tokenService.getUserToken(userEntity);
            userEntity.setToken(userToken);
            return ResponseUtils.success(userEntity);
        }
        return ResponseUtils.failed("登录失败！可能用户名密码不正确");
    }

    /**
     * 用户注册方法
     * @param username
     * @param password
     * @return
     */
    public ResponseEntity<UserEntity> register(String username,String password){
        UserEntity userEntity = new UserEntity(username, password);
        try{
            UserEntity result = userRepository.save(userEntity);
            if (result!=null){
                /**
                 * 创建账户信息
                 */
                AccountEntity accountEntity=new AccountEntity();
                accountEntity.setUserid(result.getId());
                accountEntity.setCoinamount(0.00);
                accountEntity.setAccountamount(0.00);

                AccountEntity accountResult = accountRepository.save(accountEntity);
                if (accountResult!=null){
                    long accountId = accountEntity.getId();
                    userRepository.modifyAccountId(accountId,result.getId());
                    return ResponseUtils.success(result);
                }
                return ResponseUtils.failed("账户信息创建失败");
            }
            return ResponseUtils.failed("用户信息注册失败");
        }
        catch (Exception ex){
            return ResponseUtils.failed("注册失败，error:"+ex.getMessage());
        }

    }

    /**
     * 用户是否存在
     * @param username
     * @return
     */
    public ResponseEntity<UserEntity> userExists(String username){
        String sql="select * from s_user where username='"+username+"'";
        Query query = entityManager.createNativeQuery(sql, UserEntity.class);
        UserEntity result = (UserEntity) query.getSingleResult();
        if (result!=null){
            return ResponseUtils.success(result);
        }
        return ResponseUtils.failed("当前用户不存在");
    }

    /**
     * 修改密码
     * @param newPwd
     * @param userid
     * @return
     */
    public ResponseEntity<Boolean> modifyPwd(String newPwd,int userid){
        try{
            userRepository.modifyPwd(newPwd,userid);
            return ResponseUtils.success(true);
        }catch (Exception ex){
            return ResponseUtils.failed("修改密码失败");
        }
    }

}
